﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUSService;
using DTOService;

namespace QuanLyBangDia
{
    public partial class frmTraBangDia : Form
    {
        //Thien code
        MyService myservice = new MyService();
        QuyDinh qd = new QuyDinh();
        float tongtienboithuong = 0;
        float tongtien = 0;
        float tongtientratre = 0;

        public frmTraBangDia()
        {         
            InitializeComponent();
            this.Icon = QuanLyBangDia.Properties.Resources.HDD;
        }

        private void btnTraDia_Click(object sender, EventArgs e)
        {                     
            bool flag = true;
            try
            {
                for (int i = 0; i < dgvDsBangDiaThue.Rows.Count; i++)
                {
                    string mabd=dgvDsBangDiaThue.Rows[i].Cells[2].Value.ToString();                    
                    int slview = int.Parse(dgvDsBangDiaThue.Rows[i].Cells[6].Value.ToString());
                    string makh = txtMa.Text;
                    float tientratre = 0;
                    float tienboithuong = 0;
                    BangDia_Muon bdm = new BangDia_Muon();
                    BangDia bd = new BangDia();
                    bdm = myservice.GetBangDia_Muon(mabd, makh);                    
                    bd = myservice.GetBangDia(mabd);
                    bdm.NgayTra = DateTime.Now.ToString();

                    // mat dia
                    if ((bool)dgvDsBangDiaThue.Rows[i].Cells[0].Value)
                    {
                        int soluong = bd.SoLuong - slview;
                        int kq = myservice.GhiNhanTinhTrang(mabd, soluong);
                        tienboithuong = bd.GiaBD * slview;
                        
                        bdm.TienBoiThuong += tienboithuong;
                        bdm.TongTien = bdm.TienBoiThuong + bdm.TienPhatTraTre + bdm.TienThue;

                        int kqtd = myservice.TraDia(bdm);
                        if (kq != 1|| kqtd!=1) { flag = false; break; }
                    }
                    // Tra dia
                    else if ((bool)dgvDsBangDiaThue.Rows[i].Cells[1].Value)
                    {
                        TimeSpan ts = DateTime.Now - DateTime.Parse(dgvDsBangDiaThue.Rows[i].Cells[4].Value.ToString());
                        if(((int)ts.TotalDays - qd.SoNgayThueToiDa) > 0)
                        {
                            tientratre = (float)(((int)ts.TotalDays - qd.SoNgayThueToiDa) * qd.TienPhatTraTre * slview);
                        }
                                               
                        bdm.TienPhatTraTre += tientratre;
                        bdm.TongTien = bdm.TienBoiThuong + bdm.TienPhatTraTre + bdm.TienThue;
                        
                        int kqtd = myservice.TraDia(bdm);
                        if (kqtd != 1) { flag = false; break; }
                    }
                }
                if (flag == false) { MessageBox.Show("Trả đĩa không thành công! \nKiểm tra lại!", "Trả Đĩa", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                else { 
                    MessageBox.Show("Trả đĩa thành công!", "Trả Đĩa", MessageBoxButtons.OK, MessageBoxIcon.None);
                    dgvDsBangDiaThue.Rows.Clear();
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
        }

        private void frmTraBangDia_Load(object sender, EventArgs e)
        {
            try
            {
                tongtienboithuong = 0;
                tongtien = 0;
                tongtientratre = 0;
                qd = myservice.SelectQuyDinh();
                KhachHang[] arr = myservice.SelectKhachHang();
                AutoCompleteStringCollection arrCollection = new AutoCompleteStringCollection();
                foreach (KhachHang bd in arr)
                {
                    arrCollection.Add(bd.HoTen);
                }
                txtTimTenKH.AutoCompleteMode = AutoCompleteMode.Suggest;
                txtTimTenKH.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtTimTenKH.AutoCompleteCustomSource = arrCollection;               
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                tongtienboithuong = 0;
                tongtien = 0;
                tongtientratre = 0;
                if (!String.IsNullOrEmpty(txtTimTenKH.Text))
                {
                    KhachHang[] arr = myservice.SearchKhachHang(txtTimTenKH.Text);
                    dgvDsKhachHang.DataSource = arr;
                    dgvDsKhachHang.Show();
                }
            }
            catch (Exception ex)
            { throw new Exception(ex.Message); }
        }

        private void dgvDsKhachHang_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                tongtienboithuong = 0;
                tongtien = 0;
                tongtientratre = 0;
                DataGridViewRow currentRow = dgvDsKhachHang.CurrentRow;
                txtMa.Text = currentRow.Cells[0].Value.ToString();
                txtTenKH.Text = currentRow.Cells[1].Value.ToString();
                txtDiachi.Text = currentRow.Cells[2].Value.ToString();
                txtNgaySinh.Text = currentRow.Cells[3].Value.ToString();
                txtSoDT.Text = currentRow.Cells[5].Value.ToString();
                txtTienCoc.Text = currentRow.Cells[7].Value.ToString();                
            }
            catch (Exception ex)
            { }
        }

        private void txtMa_TextChanged(object sender, EventArgs e)
        {
            try
            {
                tongtienboithuong = 0;
                tongtien = 0;
                tongtientratre = 0;
                if (!String.IsNullOrEmpty(txtMa.Text))
                {
                    dgvDsBangDiaThue.Rows.Clear();
                    BangDia_Muon[] arr;
                    arr = myservice.GetBangDia_MuonOFKhachhang(txtMa.Text);
                    foreach (BangDia_Muon item in arr)
                    {
                        for (int i = 0; i < item.SoLuong; i++)
                        {                            
                            TimeSpan ts = DateTime.Now - DateTime.Parse(item.NgayMuon);
                            if ((int)((int)ts.TotalDays - qd.SoNgayThueToiDa) > 0)
                            {
                                tongtientratre += (float)((int)((int)ts.TotalDays - qd.SoNgayThueToiDa) * qd.TienPhatTraTre);
                            }
                            BangDia bd = myservice.GetBangDia(item.MaBD);
                            Object[] o = { false, true, item.MaBD.ToString(), bd.TenBD, item.NgayMuon, item.NgayTra, 1, item.TienThue.ToString(), item.TongTien.ToString() };
                            dgvDsBangDiaThue.Rows.Add(o);
                        }
                    }
                    dgvDsBangDiaThue.Show();
                    txtTienPhatQuaHan.Text = tongtientratre.ToString();
                    tongtien += tongtientratre;
                    txtTongTien.Text = Math.Round(tongtien,0).ToString();
                }
            }
            catch (Exception ex)
            { }
        }        

        private void dgvDsBangDiaThue_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex > -1 && e.RowIndex > -1)
                {
                    if ((bool)dgvDsBangDiaThue[0, e.RowIndex].Value)
                    {                        
                        string mabd = dgvDsBangDiaThue[2, e.RowIndex].Value.ToString();
                        int soluong=int.Parse(dgvDsBangDiaThue[6,e.RowIndex].Value.ToString());
                        BangDia bd = myservice.GetBangDia(mabd);
                        tongtienboithuong += bd.GiaBD * soluong;
                        txtTienBoiThuong.Text = Math.Round(tongtienboithuong, 0).ToString();
                        tongtien += tongtienboithuong;
                        txtTongTien.Text = Math.Round(tongtien,0).ToString();
                    }
                    if (!(bool)dgvDsBangDiaThue[0, e.RowIndex].Value)
                    {
                        if (float.Parse(txtTienBoiThuong.Text) > 0)
                        {
                            string mabd = dgvDsBangDiaThue[2, e.RowIndex].Value.ToString();
                            int soluong = int.Parse(dgvDsBangDiaThue[6, e.RowIndex].Value.ToString());
                            BangDia bd = myservice.GetBangDia(mabd);
                            tongtien -= tongtienboithuong;
                            tongtienboithuong -= bd.GiaBD * soluong;
                            txtTienBoiThuong.Text = Math.Round(tongtienboithuong, 0).ToString();                            
                            txtTongTien.Text = Math.Round(tongtien, 0).ToString();
                        }
                    }                    
                }
            }
            catch (Exception ex)
            { }
        }

        private void dgvDsBangDiaThue_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgvDsBangDiaThue.CurrentCell is DataGridViewCheckBoxCell)
                    dgvDsBangDiaThue.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
            catch (Exception ex)
            { }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
